//
//  ZPUMShareShowView.m
//  ZhenPin
//
//  Created by fei on 14-7-7.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "ZPUMShareShowView.h"
//#import "WXApi.h"
#define BUTTON_TAG 300
#define BUTTON_SIZE_HEIGHT 100
#define BUTTON_SIZE_WIDTH  80
#define BUTTON_ITEM_SPACE 20
#define BUTTON_LINE_SPACE 10

static ZPUMShareShowView *shareView;
@implementation ZPUMShareShowView
{
    BOOL isWXInstalled;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

+(ZPUMShareShowView *)shareView {
    if (!shareView) {
        shareView = [[ZPUMShareShowView alloc] initWithFrame:CGRectZero];
        [shareView setSubViews];
    }
    return shareView;
}

-(void)setSubViews {
//    float height = 0;
//    shareView.frame = [UIScreen mainScreen].bounds;
//    shareView.backgroundColor = [UIColor whiteColor];
//    shareItemArray = [NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline, nil];
//    isWXInstalled = [WXApi isWXAppInstalled];
//    NSArray *titleArray;
//    NSArray *imgarr;
//    if (isWXInstalled) {
//        titleArray = [NSArray arrayWithObjects:@"新浪微博",@"微信好友",@"微信朋友圈",@"邮箱",@"短信", nil];
//        imgarr = [NSArray arrayWithObjects:@"weibo",@"weixin",@"pengyouquan",@"emall",@"duanxin", nil];
//        
//    } else {
//        titleArray = [NSArray arrayWithObjects:@"新浪微博",@"邮箱",@"短信", nil];
//        imgarr = [NSArray arrayWithObjects:@"weibo",@"emall",@"duanxin", nil];
//    }
//    
//    
//    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 320, 20)];
//    titleLabel.backgroundColor = self.backgroundColor;
//    
//    titleLabel.textAlignment = NSTextAlignmentCenter;
//    titleLabel.text = @"分享页面到";
//    
//    [shareView addSubview:titleLabel];
//    height += 25;
//    for (int i = 0; i < titleArray.count; i ++) {
//        UIButton *clickButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        clickButton.frame = CGRectMake(20+(BUTTON_SIZE_WIDTH + BUTTON_ITEM_SPACE)*(i%3), 40+(BUTTON_SIZE_HEIGHT + BUTTON_LINE_SPACE)*(i/3), BUTTON_SIZE_WIDTH, BUTTON_SIZE_HEIGHT);
//        [clickButton setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
//        [clickButton setImage:[UIImage imageNamed:[imgarr objectAtIndex:i]] forState:UIControlStateNormal];
//        if (i <= 2) {
//            
//        }
//        [clickButton setImageEdgeInsets:UIEdgeInsetsMake(-20, 15, 0, 0)];
//        [clickButton setTitleEdgeInsets:UIEdgeInsetsMake(70, -48, 10, 0)];
//        clickButton.titleLabel.font = [UIFont systemFontOfSize:12];
//        [clickButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        clickButton.tag  = BUTTON_TAG + i;
//        clickButton.clipsToBounds = YES;
//        
//        [clickButton addTarget:self action:@selector(shareButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//        [shareView addSubview:clickButton];
//        height = clickButton.frame.origin.y + BUTTON_SIZE_HEIGHT + BUTTON_LINE_SPACE;
//    }
//    
//    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    cancelButton.frame = CGRectMake(0, height, 320, 30);
//    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [cancelButton setBackgroundColor:self.backgroundColor];
//    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
//    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//    [shareView addSubview:cancelButton];
//    height += 30;
//    shareView.frame =CGRectMake(0, [UIScreen mainScreen].bounds.size.height, 320, height);
    
    
}
-(void)shareButtonClicked:(UIButton *)clickedBtn {
    
    
    
    int  tag = clickedBtn.tag-300;
    
    //加载微信appKey后 sina,tencent,wxsession,wxtimeline,wxfavorite,qzone,renren,douban,email,sms,facebook,twitter
    NSArray *array ;
    if (isWXInstalled) {
        array = [NSArray arrayWithObjects:@"sina",@"wxsession",@"wxtimeline",@"email",@"sms", nil];
    } else {
        array = [NSArray arrayWithObjects:@"sina",@"email",@"sms", nil];
    }
    NSString *snsName = [array objectAtIndex:tag];
    
    NSString *text;
    NSString *title;
    UIImage *image;
    
    if ([snsName isEqualToString:@"sms"] || [snsName isEqualToString:@"email"]) {
        title = self.shareTitle;
        text = self.shareText;
        image = nil;
    } else {
        if ([snsName isEqualToString:@"sina"]) {
            text = [NSString stringWithFormat:@"%@",self.shareText];
        }
        title = [NSString stringWithFormat:@"%@  %@",self.shareTitle,self.shareText];
        image = self.shareImage;
        
    }
/*
    [UMSocialData defaultData].shareText = text;
    [UMSocialData defaultData].extConfig.title = title;
    [UMSocialData defaultData].shareImage = image;
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
    
    snsPlatform.snsClickHandler(_superViewController,[UMSocialControllerService defaultControllerService],YES);
*/
    [self hideShareView];
    
    
}
-(void)showToFrame {
    shareView.alpha = 1.0;
    _backControl = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if (!keyWindow) {
        keyWindow = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }
    _backControl.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [_backControl addGestureRecognizer:tap];
    
    [keyWindow addSubview:_backControl];
    [keyWindow addSubview:self];
    CGRect rect = [[UIScreen mainScreen] bounds];
    [UIView animateWithDuration:.5 animations:^{
        shareView.frame = CGRectMake(0, rect.size.height-shareView.frame.size.height, rect.size.width, shareView.frame.size.height);
    } completion:^(BOOL isFinished) {

    }];
}
-(void)tapGesture:(UITapGestureRecognizer *)tap {
    [self hideShareView];
}
-(void)cancelButtonClicked {
    [self hideShareView];
}

-(void)hideShareView {
    [UIView animateWithDuration:0.35 animations:^{
        shareView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, shareView.frame.size.width, shareView.frame.size.height);
    } completion:^(BOOL isFinished) {
        if (isFinished) {
            [_backControl removeFromSuperview];
            shareView.alpha = 0;
        }
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
